package com.zhongkun.datahome.web.app;

import com.zhongkun.datahome.common.core.domain.R;
import com.zhongkun.datahome.common.core.domain.entity.SysUser;
import com.zhongkun.datahome.common.core.domain.entity.TenantUser;
import com.zhongkun.datahome.common.core.domain.model.LoginUser;
import com.zhongkun.datahome.common.utils.SecurityUtils;
import com.zhongkun.datahome.core.repository.TenantRepository;
import com.zhongkun.datahome.service.res.setting.AppDataSettingVo;
import com.zhongkun.datahome.service.res.setting.AppVisionSettingVo;
import com.zhongkun.datahome.service.service.ISystemSettingService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;
import java.util.Objects;

/**
 * @Author: lifengsheng
 * @Date: 2025/5/8 11:29
 * @Desc
 */
@Api("小程序接口")
@RestController
@RequestMapping("/app/setting")
public class AppSettingController {
    @Resource
    private ISystemSettingService systemSettingService;

    @Resource
    private TenantRepository tenantRepository;

    @ApiOperation("获取商户名称")
    @GetMapping("/getTenantName")
    public R<String> getTenantName() {
        LoginUser loginUser = SecurityUtils.getLoginUser();
        if (Objects.isNull(loginUser) || Objects.isNull(loginUser.getTenantId())) {
            return R.fail("用户未登陆");
        }

        return R.ok(tenantRepository.getName(loginUser.getTenantId()));
    }

    @ApiOperation("获取商户小程序视觉配置")
    @GetMapping("/getVisionSettings")
    public R<List<AppVisionSettingVo>> getVisionSettings(Long tenantId) {
        if (tenantId != null) {
            SecurityUtils.setTenantId(tenantId);
        }
        return R.ok(systemSettingService.getMiniProgramVisionSettings());
    }

    @ApiOperation("获取商户小程序数据图标配置")
    @GetMapping("/getDataSettings")
    public R<List<AppDataSettingVo>> getDataSettings(@RequestParam(required = false) Long tenantId,
                                                     @RequestParam(required = false) Boolean employeeFlag) {
        if (tenantId != null) {
            SecurityUtils.setTenantId(tenantId);
        }
        return R.ok(systemSettingService.getMiniProgramDataSettings(employeeFlag));
    }
}
